import React, {useEffect} from 'react';
import {Select,Form} from "antd";
import {FormInstance} from "antd/lib";
import LogFactory from "log/LogFactory";
import {OutboundDeliveryOrder} from "type/OutboundDeliveryOrder";

import {ODO_OPTIONS, RETURN, SALE, SERVICE, TRANSFER} from "../../constant";
import {OdoTypeKey, OdoTypeSelectProps} from "../../init";
import {OtherOdoSelect, ReturnOdoSelect, SaleOdoSelect, ServiceOdoSelect, TransferOdoSelect} from "./select/init";
import DrpSettingHandler from "../../DrpSettingHandler";

const log = LogFactory.getLog("OdoTypeSelect");
/**
 * 根据出库类型获取对应的表单项
 * @param odoType 出库类型
 * @param form 表单实例
 */
const getFormItemByOdoType = (odoType: OdoTypeKey,form: FormInstance<OutboundDeliveryOrder>): React.ReactNode => {
    switch (odoType) {
        case SALE:
            return <SaleOdoSelect form={form}/> ;
        case SERVICE:
            return <ServiceOdoSelect form={form}/> ;
        case TRANSFER:
            return <TransferOdoSelect form={form}/>;
        case RETURN:
            return <ReturnOdoSelect form={form}/>;
        default:
            return <OtherOdoSelect form={form}/> ;
    }
} ;

const OdoTypeSelect = ({form}: OdoTypeSelectProps) => {

    useEffect(() => {
        form.setFieldValue("odoType",DrpSettingHandler.getInstance().getConfig().defaultOdoType) ;
    }, []);
    const initState = (): React.ReactNode => {
        return getFormItemByOdoType(DrpSettingHandler.getInstance().getConfig().defaultOdoType,form) ;
    } ;

    const [flowComponent,setFlowComponent]
        = React.useState<React.ReactNode>(initState) ;

    const selectChange = (value: OdoTypeKey) => {
        log.debug("选择的值: ",value) ;
        setFlowComponent(getFormItemByOdoType(value,form)) ;
    } ;

    return (
        <>
            <Form.Item<OutboundDeliveryOrder> label={"出库类型"} name={"odoType"} required>
                <Select onChange={selectChange} options={ODO_OPTIONS}/>
            </Form.Item>
            {flowComponent}
        </>
    );
};

export default OdoTypeSelect;